package hangUpAndReply;

/**
 * 使用 wait 和 notify
 * wait 会释放资源 所以不会造成死锁
 */
public class WaitAndNotify implements Runnable {

    public static Object o = new Object();

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (o) {
            System.out.println(name + ">" + "已经获取到锁");
            try {
                o.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name + ">" + "被唤醒");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new WaitAndNotify(),"线程1");
        Thread thread2 = new Thread(new WaitAndNotify(),"线程2");

        thread1.start();
        thread2.start();
        Thread.sleep(3000);
        synchronized (o){
            o.notify();
        }



    }
}
